* Set directories
cd /Users/byrned/Dropbox/Research/InformedSources/Replication/Analysis/
local datdir /Users/byrned/Dropbox/Research/InformedSources/Replication/Build/Output/
est clear

*-------------------- Construct set of pairwise neighbours --------------------*


* Load data, apply sample restrictions
use `datdir'is_mel_p_2005_2019, clear
keep if t>=td(1may2015) & t<=td(31dec2017)

* Lat/Lon for each station
sort id t
by id: keep if _n==1
drop if coor==""
gen m1=strpos(coor,",")-2
gen m2=strpos(coor,",")+2
gen m3=strpos(coor,")")
gen len=m3-m2
gen lat1=substr(coor,2,m1)
gen lon1=substr(coor,m2,len)
keep id lat1 lon1
destring lat1, force replace
destring lon1, force replace
rename id id1
save Temp/temp1, replace

* Find rivals within 1km of each station
qui sum id1
local nid=r(N)
expand `nid'
sort id1
by id1: gen num=_n
save Temp/temp2, replace

levelsof id1, local(levels) 
foreach i of local levels {
	use Temp/temp1, clear
	rename id1 id2
	rename lat1 lat2
	rename lon1 lon2
	gen double id1=`i'
	sort id1 id2
	by id1: gen num=_n
	sort id1 num
	qui merge 1:1 id1 num using Temp/temp2, nogen
	sort id1 num
	save Temp/temp2, replace	
}

* Compute distance between all pairs
local rad=3.141592653589/180
gen dist=acos(sin(lat1*`rad')*sin(lat2*`rad')+cos(lat1*`rad')*cos(lat2*`rad')*cos(lon2*`rad'-lon1*`rad'))*6371
order num id1 lat1 lat1 id2 lat2 lon2 dist
sort id1 dist

* Pairwise neigbours	
* 1km - 880, 1.5km - 1598, 2km - 2712
drop if id1==id2
local nei=1
keep if dist<=`nei'
save Temp/temp2, replace



*----------------- Construct dataset with rival price changes -----------------*

* 1. Identify price changes by rivals j
* 2. Keep firm i responses to price changes by rivals


local first=1
use Temp/temp2, clear
levelsof id1, local(levels) 
foreach i of local levels {
	display `i'
	* Get firm i's nearest rival
	use Temp/temp2, clear
	keep if id1==`i'
	sort dist id2
	keep if _n==1
	rename id2 id
	keep id
	sort id
	save Temp/temp3, replace
	
	* Get firm i's nearest rival's prices
	use `datdir'is_mel_p_2005_2019, clear
	keep if t>=td(1may2015) & t<=td(31dec2017)
	sort id
	qui merge m:1 id using Temp/temp3, keep(match)
	keep t dp bid p
	rename p p_riv
	rename dp dp_riv
	rename bid bid_riv
	sort t
	save Temp/temp3, replace

	* Get firm i's prices
	use `datdir'is_mel_p_2005_2019, clear
	keep if id==`i'
	keep if t>=td(1may2015) & t<=td(31dec2017)
	sort t
	qui merge 1:1 t using Temp/temp3, nogen
	sort id t
	
	* Save regression dataset
	if(`first'!=1){
		qui merge m:m id t using Temp/regression_data, nogen
	}
	local first=0
	save Temp/regression_data, replace
	
}


*----------------- Estimate price reactions pre/post by brand -----------------*

* Load data
use Temp/regression_data, clear
drop if bid==6

* Brand dummies
gen bp=cond(bid==1,1,0)
gen cal=cond(bid==2,1,0)
gen coles=cond(bid==3,1,0)
gen wool=cond(bid==4,1,0)
gen sev=cond(bid==5,1,0)

* Coles off the platform
gen off=cond(t>td(15mar2016),1,0)
gen treat=coles*off

* Pre/post regression
gen ch=cond(dp!=0 & dp!=.,1,0)
gen ch_riv=cond(dp_riv!=0 & dp_riv!=.,1,0)
gen ch_riv_coles=ch_riv*coles

gen ch_riv_off=ch_riv*off
gen ch_riv_off_coles=ch_riv*off*coles

* Time dummies
gen dow=dow(t)
gen moy=mofd(t)
gen woy=wofd(t)

reghdfe ch ch_riv, noabsorb cluster(id t)
eststo m1
reghdfe ch ch_riv ch_riv_coles ch_riv_off ch_riv_off_coles coles off, noabsorb cluster(id t)
eststo m2
reghdfe ch ch_riv ch_riv_coles ch_riv_off ch_riv_off_coles, absorb(i.id i.dow i.woy) cluster(id t)
eststo m3

esttab m1 m2 m3 using "Output/fig8_commit.tex", replace b(3) se(3) ///
	keep(ch_riv ch_riv_coles ch_riv_off ch_riv_off_coles coles off _cons) ///
	order(ch_riv ch_riv_coles ch_riv_off ch_riv_off_coles coles off _cons,) ///
	varlabel(ch_riv "\$PriceChange_{j}$" ///
			 ch_riv_coles "\$PriceChange_{j} \times Coles_{i}\$" ///
			 ch_riv_off "\$PriceChange_{j} \times ColesOff_{t}\$" ///
			 ch_riv_off_coles "\$PriceChange_{j} \times Coles_{i} \times ColesOff_{t}\$" ///
			 coles "\$Coles_{i}\$" ///
			 off "\$ColesOff_{t}\$" ///
			 _cons "Constant")	///
	stats(r2_a N, labels("R-Squared" "Observations") fmt(%12.2f %12.0f)) ///
	starl($^{*}$ .1 $^{**}$ .05 $^{***}$ .01) ///	
	booktabs nonote sty(tex)
eststo clear	

* Graphs with change in price sensitivity
reghdfe ch ch_riv ch_riv_coles ch_riv_off ch_riv_off_coles coles off, noabsorb cluster(id t)
tempname coef_plots
postfile `coef_plots' coef std lb ub val using Temp/commit, replace
qui lincom _cons + ch_riv
post `coef_plots' (r(estimate)) (r(se)) (r(lb)) (r(ub)) (3) 
qui lincom _cons + ch_riv + ch_riv_off + off
post `coef_plots' (r(estimate)) (r(se)) (r(lb)) (r(ub)) (4)
qui lincom _cons + ch_riv + ch_riv_coles + coles
post `coef_plots' (r(estimate)) (r(se)) (r(lb)) (r(ub)) (1)
qui lincom _cons + ch_riv + ch_riv_coles + coles + ch_riv_off + ch_riv_off_coles + off
post `coef_plots' (r(estimate)) (r(se)) (r(lb)) (r(ub)) (2)
postclose `coef_plots'

use Temp/commit, clear
twoway 	(bar coef val if val==1, color(gs0*0.2)) ///
		(bar coef val if val==2, color(gs0*0.4)) ///
		(bar coef val if val==3, color(gs*0.8)) ///
		(bar coef val if val==4, color(gs*1)) /// 
		(rcap lb ub val if val==1, lcolor(gs0)) ///
		(rcap lb ub val if val==2, lcolor(gs0)) ///
		(rcap lb ub val if val==3, lcolor(gs0)) ///
		(rcap lb ub val if val==4, lcolor(gs0) ///
		ylabel(0(0.1)1) xlabel(none) ///
		ytitle("Price Change Sensivity to" "Local Rival Price Changes") ///
		xtitle("") ///
		plotregion(margin(zero) style(none) fcolor(white)) graphregion(color(white)) bgcolor(white) ///
		legend(size(*0.7) position(12) symx(*0.5) ///
		region(lwidth(*0.05)) rows(2) order(1 3 2 4) ///
		label(1 "Coles Stations" "Coles {bf:ON} Platform") ///
		label(2 "Coles Stations" "Coles {bf:OFF} Platform") ///
		label(3 "Other Retailers' Stations" "Coles {bf:ON} Platform") ///
		label(4 "Other Retailers' Stations" "Coles {bf:OFF} Platform")))		
graph export Output/fig8_commit.pdf, as(pdf) replace		
		
		
 




